package logger

import (
	"errors"
	"fmt"
	"gopkg.in/natefinch/lumberjack.v2"
	"time"
)

const (
	LEVEL_DEBUG = uint8(iota)
	LEVEL_INFO
	LEVEL_WARN
	LEVEL_ERROR
)

var levelMap = map[string]uint8{
	"debug": LEVEL_DEBUG,
	"info":  LEVEL_INFO,
	"warn":  LEVEL_WARN,
	"error": LEVEL_ERROR,
}

func NewLogger(rootPath string, level string, appName string) (*LoggerContext, error) {
	if rootPath == "" {
		return nil, errors.New("log初始化全局配置失败,参数rootPath不能为空")
	}
	if _, ok := levelMap[level]; !ok {
		return nil, errors.New("log初始化全局配置失败,参数level错误")
	}
	if appName == "" {
		return nil, errors.New("log初始化全局配置失败,参數appName不能为空")
	}

	return new(LoggerContext).NewLoggerWithLevel(), nil
}

func newLumberjackSink() *lumberjack.Logger {
	//logFilePath := fmt.Sprintf("%s/%s/%s/log", globalRootPath, loggerType, globalAppName)
	globalRootPath := "./"
	loggerType := "./log"
	project := "shop-admin"
	globalAppName := time.Now().Format("2006-01-02")
	logFilePath := fmt.Sprintf("%s/%s/%s/%s.log", globalRootPath, loggerType, project, globalAppName)
	return &lumberjack.Logger{
		//Filename:   "./logs/spikeProxy1.log", // 日志文件路径
		Filename:   logFilePath, // 日志文件路径
		MaxSize:    128,         // 每个日志文件保存的最大尺寸 单位：M
		MaxBackups: 30,          // 日志文件最多保存多少个备份
		MaxAge:     7,           // 文件最多保存多少天
		Compress:   true,        // 是否压缩
	}
}
